package com.studentsystem.controller;

import com.studentsystem.common.Result;
import com.studentsystem.dataobject.GradeDO;
import com.studentsystem.dataobject.StudentDO;
import com.studentsystem.service.GradeService;
import com.studentsystem.service.StudentService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Grade)表控制层
 */
@RestController
@RequestMapping("grade")
public class GradeController {
    /**
     * 服务对象
     */
    @Resource
    private GradeService gradeService;

    @Resource
    private StudentService studentService;

    @PostMapping("add")
    public Result<String> add(@RequestBody GradeDO gradeDO) {
        gradeDO.setCreatedTime(LocalDateTime.now());
        gradeDO.setModifiedTime(LocalDateTime.now());
        Boolean insert = gradeService.insert(gradeDO);
        if (!insert) {
            return Result.fail("添加失败", "error");
        } else {
            return Result.success("success");
        }
    }

    @PutMapping("update")
    public Result<String> update(@RequestBody GradeDO gradeDO) {
        gradeDO.setModifiedTime(LocalDateTime.now());
        Boolean update = gradeService.update(gradeDO);
        if (!update) {
            return Result.fail("添加失败", "error");
        } else {
            return Result.success("success");
        }
    }

    @GetMapping("getInfo")
    public Result<List<GradeDO>> getInfo(@RequestParam("studentNumber") String studentNumber,
                                         @RequestParam("year") Integer year,
                                         @RequestParam("term") Integer term) {
        return Result.success(this.gradeService.queryGradeInfo(studentNumber, year, term));
    }

    @GetMapping("getAll")
    public Result<List<GradeDO>> getAll(@RequestParam("classs") String classs,
                                        @RequestParam("term") Integer term,
                                        @RequestParam("courseName") String courseName) {
        return Result.success(this.gradeService.queryAll(classs, term, courseName));
    }

    @GetMapping("getGradeInfoByClass")
    public Result<Map<String, Object>> getGradeInfoByClass(@RequestParam("classs") String classs,
                                                           @RequestParam("grade") Integer grade,
                                                           @RequestParam("courseName") String courseName) {
        if (StringUtils.isEmpty(classs) || StringUtils.isEmpty(courseName)) {
            return Result.fail("班级或课程名为空", null);
        }
        List<StudentDO> all = studentService.queryByClasssAndGrade(classs, grade);
        Map<String, GradeDO> added = gradeService.queryAddedStudents(classs, courseName);

        Map<String, Object> map = new HashMap<>(2);
        map.put("all", all);
        map.put("added", added);
        return Result.success(map);
    }

}